import { ISubscriber } from "../event/event_switch";
import { Timer } from "./timer";

type OnTickEventCallbackType = (arg0?: any) => void;

export class OnTickEvent implements ISubscriber {
  timer?: Timer;
  duration?: number;
  interval?: number;
  constructor(interval?: number, duration?: number) {
    this.interval = interval;
    this.duration = duration;
  }

  subscribe(callback: OnTickEventCallbackType, options?: any): OnTickEventCallbackType {
    this.timer = new Timer(callback, this.interval);

    if (this.duration != undefined) {
      this.timer.start_interval_for(this.duration);
    } else {
      this.timer.start_interval();
    }

    return callback;
  }

  unsubscribe(callback?: OnTickEventCallbackType): void {
    this.timer?.stop();
  }
}
